package servlet;

import java.io.IOException;
import java.sql.Date;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import decoder.ChineseDecoder;

import model.Goods;

import factory.DaoFactory;

/**
 * Servlet implementation class ModifyGoodsServlet
 */
@WebServlet("/ModifyGoodsServlet")
public class ModifyGoodsServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public ModifyGoodsServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String name = ChineseDecoder.getUtf8FromIso8859_1(request.getParameter("name"));
		int id = Integer.parseInt(request.getParameter("id"));
		DateFormat fmt =new SimpleDateFormat("yyyy-MM-dd");
		java.util.Date d = null;
		try {
			d = fmt.parse(request.getParameter("date"));
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		Date date = new Date(d.getTime());
		Goods goods = DaoFactory.getGoodsDao().find(id, date);
		request.setAttribute("goods", goods);
		String address = "/jsp/ModifyGoods.jsp?name=" + name;
		RequestDispatcher dispatcher =request.getRequestDispatcher(address);
		dispatcher.forward(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String name = ChineseDecoder.getUtf8FromIso8859_1(request.getParameter("name"));
		int goodsId = Integer.parseInt(request.getParameter("goodsId"));
		String goodsName = ChineseDecoder.getUtf8FromIso8859_1(request.getParameter("goodsName"));
		float price = 0;
		int quantity = 0;

		try {
			price = Float.parseFloat(request.getParameter("price"));
			if (price < 0) {
				response.sendRedirect(request.getContextPath() + "/jsp/ErrorPage.jsp?error=MdfGoodsPriceError&name=" + name);
				return;
			}
		} catch(NumberFormatException e) {
			response.sendRedirect(request.getContextPath() + "/jsp/ErrorPage.jsp?error=MdfGoodsPriceError&name=" + name);
			return;
		}

		try {
			quantity = Integer.parseInt(request.getParameter("quantity"));
			if (quantity < 0) {
				response.sendRedirect(request.getContextPath() + "/jsp/ErrorPage.jsp?error=MdfGoodsQuantityError&name=" + name);
				return;
			}
		} catch(NumberFormatException e) {
			response.sendRedirect(request.getContextPath() + "/jsp/ErrorPage.jsp?error=MdfGoodsQuantityError&name=" + name);
			return;
		}

		Date date = null;
		DateFormat fmt =new SimpleDateFormat("yyyy-MM-dd");
		java.util.Date d = null;
		try {
			d = fmt.parse(request.getParameter("date"));
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		date = new Date(d.getTime());

		Goods goods = new Goods(goodsId, goodsName, price, quantity, date);
		boolean success = DaoFactory.getGoodsDao().updateByIdDate(goods);

		if (success) {
			response.sendRedirect(request.getContextPath() + "/GoodsManagementServlet?name=" + name);
		} else {
			response.sendRedirect(request.getContextPath() + "/jsp/ErrorPage.jsp?error=MdfGoodsDatabaseError&name=" + name);
		}

	}

}
